#include <iostream>
#include <signal.h>

void PrintSigset_t(sigset_t sigset)
{
    for (int i = 31; i >= 1; i--)
        if (sigismember(&sigset, i))
            std::cout << 1;
        else
            std::cout << 0;
    std::cout << std::endl;
}
void sigcb(int signal) // callback回调
{
    while (true)
    {
        std::cout << "signal processing:" << signal << ", pending: ";
        sigset_t pending;
        sigpending(&pending);
        PrintSigset_t(pending);
        sleep(1);
    }
}

int main()
{
    struct sigaction act, oldact;
    sigemptyset(&act.sa_mask);        // 初始化
    sigaddset(&act.sa_mask, SIGQUIT); // 可以再sa_mask中添加更多的屏蔽信号
    act.sa_flags = 0;
    act.sa_handler = sigcb;
    int re = sigaction(SIGINT, &act, &oldact);
    if (re == 0)
    {
        std::cout << "sigaction success" << std::endl;
    }
    else if (re == -1)
    {
        std::cout << "sigaction fail" << std::endl;
    }
    while (true)
    {
        std::cout << "I am a process, pid: " << getpid() << ", pending: ";
        sigset_t pending;
        sigpending(&pending);
        PrintSigset_t(pending);
        sleep(1);
    }

    return 0;
}